using ns0;
using ns4;
using System;
using System.Collections.Generic;
namespace ns5
{
	internal sealed class Class6
	{
		private BitMatrix bitMatrix_0;
		private List<object> list_0;
		private int int_0;
		private int int_1;
		private int int_2;
		private int int_3;
		private float float_0;
		private int[] int_4;
		private ResultPointCallback resultPointCallback_0;
		internal Class6(BitMatrix bitMatrix_1, int int_5, int int_6, int int_7, int int_8, float float_1, ResultPointCallback resultPointCallback_1)
		{
			this.bitMatrix_0 = bitMatrix_1;
			this.list_0 = new List<object>(5);
			this.int_0 = int_5;
			this.int_1 = int_6;
			this.int_2 = int_7;
			this.int_3 = int_8;
			this.float_0 = float_1;
			this.int_4 = new int[3];
			this.resultPointCallback_0 = resultPointCallback_1;
		}
		internal AlignmentPattern method_0()
		{
			int num = this.int_0;
			int num2 = this.int_3;
			int num3 = num + this.int_2;
			int num4 = this.int_1 + (num2 >> 1);
			int[] array = new int[3];
			for (int i = 0; i < num2; i++)
			{
				int int_ = num4 + (((i & 1) == 0) ? (i + 1 >> 1) : (-(i + 1 >> 1)));
				array[0] = 0;
				array[1] = 0;
				array[2] = 0;
				int j = num;
				while (j < num3 && !this.bitMatrix_0.get_Renamed(j, int_))
				{
					j++;
				}
				int num5 = 0;
				while (j < num3)
				{
					if (this.bitMatrix_0.get_Renamed(j, int_))
					{
						if (num5 == 1)
						{
							array[num5]++;
						}
						else
						{
							if (num5 == 2)
							{
								if (this.method_1(array))
								{
									AlignmentPattern alignmentPattern = this.method_3(array, int_, j);
									if (alignmentPattern != null)
									{
										AlignmentPattern result = alignmentPattern;
										return result;
									}
								}
								array[0] = array[2];
								array[1] = 1;
								array[2] = 0;
								num5 = 1;
							}
							else
							{
								array[++num5]++;
							}
						}
					}
					else
					{
						if (num5 == 1)
						{
							num5++;
						}
						array[num5]++;
					}
					j++;
				}
				if (this.method_1(array))
				{
					AlignmentPattern alignmentPattern = this.method_3(array, int_, num3);
					if (alignmentPattern != null)
					{
						AlignmentPattern result = alignmentPattern;
						return result;
					}
				}
			}
			if (this.list_0.get_Count() != 0)
			{
				AlignmentPattern result = (AlignmentPattern)this.list_0.get_Item(0);
				return result;
			}
			throw ReaderException.Instance;
		}
		private static float smethod_0(int[] int_5, int int_6)
		{
			return (float)(int_6 - int_5[2]) - (float)int_5[1] / 2f;
		}
		private bool method_1(int[] int_5)
		{
			float num = this.float_0;
			float num2 = num / 2f;
			bool result;
			for (int i = 0; i < 3; i++)
			{
				if (Math.Abs(num - (float)int_5[i]) >= num2)
				{
					result = false;
					return result;
				}
			}
			result = true;
			return result;
		}
		private float method_2(int int_5, int int_6, int int_7, int int_8)
		{
			BitMatrix bitMatrix = this.bitMatrix_0;
			int height = bitMatrix.Height;
			int[] array = this.int_4;
			array[0] = 0;
			array[1] = 0;
			array[2] = 0;
			int num = int_5;
			while (num >= 0 && bitMatrix.get_Renamed(int_6, num) && array[1] <= int_7)
			{
				array[1]++;
				num--;
			}
			float result;
			if (num < 0 || array[1] > int_7)
			{
				result = float.NaN;
			}
			else
			{
				while (num >= 0 && !bitMatrix.get_Renamed(int_6, num) && array[0] <= int_7)
				{
					array[0]++;
					num--;
				}
				if (array[0] > int_7)
				{
					result = float.NaN;
				}
				else
				{
					num = int_5 + 1;
					while (num < height && bitMatrix.get_Renamed(int_6, num) && array[1] <= int_7)
					{
						array[1]++;
						num++;
					}
					if (num == height || array[1] > int_7)
					{
						result = float.NaN;
					}
					else
					{
						while (num < height && !bitMatrix.get_Renamed(int_6, num) && array[2] <= int_7)
						{
							array[2]++;
							num++;
						}
						if (array[2] > int_7)
						{
							result = float.NaN;
						}
						else
						{
							int num2 = array[0] + array[1] + array[2];
							if (5 * Math.Abs(num2 - int_8) >= 2 * int_8)
							{
								result = float.NaN;
							}
							else
							{
								result = (this.method_1(array) ? Class6.smethod_0(array, num) : float.NaN);
							}
						}
					}
				}
			}
			return result;
		}
		private AlignmentPattern method_3(int[] int_5, int int_6, int int_7)
		{
			int int_8 = int_5[0] + int_5[1] + int_5[2];
			float num = Class6.smethod_0(int_5, int_7);
			float num2 = this.method_2(int_6, (int)num, 2 * int_5[1], int_8);
			AlignmentPattern result;
			if (!float.IsNaN(num2))
			{
				float num3 = (float)(int_5[0] + int_5[1] + int_5[2]) / 3f;
				int count = this.list_0.get_Count();
				for (int i = 0; i < count; i++)
				{
					AlignmentPattern alignmentPattern = (AlignmentPattern)this.list_0.get_Item(i);
					if (alignmentPattern.method_0(num3, num2, num))
					{
						result = new AlignmentPattern(num, num2, num3);
						return result;
					}
				}
				ResultPoint resultPoint = new AlignmentPattern(num, num2, num3);
				this.list_0.Add(resultPoint);
				if (this.resultPointCallback_0 != null)
				{
					this.resultPointCallback_0.foundPossibleResultPoint(resultPoint);
				}
			}
			result = null;
			return result;
		}
	}
}
